//
//  DRCookSocialCell.m
//  DoctorRen
//
//  Created by Lemon on 2018/11/21.
//

#import "DRCookSocialCell.h"
#import "DRCookSociaModel.h"


@interface DRCookSocialCell()
@property (weak, nonatomic) IBOutlet UIImageView *avatarIV;
@property (weak, nonatomic) IBOutlet UILabel *userNameLB;
@property (weak, nonatomic) IBOutlet UIView *imageContentView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *dateLabel;
@property (weak, nonatomic) IBOutlet UIButton *likeButton;
@property (weak, nonatomic) IBOutlet UIButton *commentsButton;

@property (weak, nonatomic) IBOutlet UIImageView *type1_IV;
@property (weak, nonatomic) IBOutlet UIImageView *type2_IV1;
@property (weak, nonatomic) IBOutlet UIImageView *type2_IV2;

@property (weak, nonatomic) IBOutlet UIImageView *type3_IV1;
@property (weak, nonatomic) IBOutlet UIImageView *type3_IV2;
@property (weak, nonatomic) IBOutlet UIImageView *type3_IV3;

@property (nonatomic, strong) DRCookSociaModel *model;

@end

@implementation DRCookSocialCell

-(void)awakeFromNib{
    [super awakeFromNib];
    self.avatarIV.layer.cornerRadius = 20;
    self.avatarIV.layer.masksToBounds = YES;

    [self.likeButton addTarget:self action:@selector(clickLikeButton) forControlEvents:(UIControlEventTouchUpInside)];
    [self.commentsButton addTarget:self action:@selector(clickCommentButton) forControlEvents:UIControlEventTouchUpInside];

}

-(void)setData:(DRCookSociaModel *)data{
    self.model = data;
    [self.avatarIV setImageWithURLString:data.user_pic_id];
    self.userNameLB.text = data.user_name;
    self.dateLabel.text = data.create_time;
    [self.likeButton setTitle:data.good forState:(UIControlStateNormal)];
    [self.commentsButton setTitle:data.comments forState:UIControlStateNormal];
    self.titleLabel.text = data.content;
    BOOL isType1 = data.pic_ids.count == 1;
    BOOL isType2 = data.pic_ids.count == 2;
    BOOL isType3 = data.pic_ids.count == 3;
    if (isType1) {
        [self setImageWithUrl:data.pic_ids[0] imageView:self.type1_IV];
    }else if (isType2){
        [self setImageWithUrl:data.pic_ids[0] imageView:self.type2_IV1];
        [self setImageWithUrl:data.pic_ids[1] imageView:self.type2_IV2];
    }else if (data.pic_ids.count == 3){
        [self setImageWithUrl:data.pic_ids[0] imageView:self.type3_IV1];
        [self setImageWithUrl:data.pic_ids[1] imageView:self.type3_IV2];
        [self setImageWithUrl:data.pic_ids[2] imageView:self.type3_IV3];
    }
    [self.type1_IV setHidden:!isType1];
    [self.type2_IV1 setHidden:!isType2];
    [self.type2_IV2 setHidden:!isType2];
    [self.type3_IV1 setHidden:!isType3];
    [self.type3_IV2 setHidden:!isType3];
    [self.type3_IV3 setHidden:!isType3];
    [self.likeButton setImage:[UIImage imageNamed:data.hascollection ? @"煮友圈_点赞_1" :@"煮友圈_点赞_0"] forState:UIControlStateNormal];
}

-(void) setImageWithUrl:(NSString *) url imageView:(UIImageView *) iv{
//     url.lastPathComponent
    if ([@[@"mov",@"mp4",@"avi"] containsObject:url.pathExtension.lowercaseString]){
        //视频
        UIImage *image = self.model.videoImages[url];
        if (!image) {
            dispatch_async(dispatch_get_global_queue(0, 0), ^{
                UIImage *image = [iv setVideoPreViewImage:url];
                dispatch_async(dispatch_get_main_queue(), ^{
                    self.model.videoImages[url] = image;
                    iv.image = image;
                });
            });
        }else{
            iv.image = image;
        }

        
    }else{
        //图片
        [iv setImageWithURLString:url];
    }
    
    
}

-(void)willMoveToSuperview:(UIView *)newSuperview{
    [super willMoveToSuperview:newSuperview];

}


-(void) clickLikeButton{
    if (self.likeAction) {
        self.likeAction();
    }
}

-(void) clickCommentButton {
    if (self.commentAction) {
        self.commentAction();
    }
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [[event allTouches] anyObject];
    if (touch.view == self.type1_IV || touch.view == self.type2_IV1 || touch.view == self.type3_IV1) {
        if (self.tapAction) {
            self.tapAction(self.model.pic_ids, 0);
        }
    }else if (touch.view == self.type2_IV2 || touch.view == self.type3_IV2){
        if (self.tapAction) {
            self.tapAction(self.model.pic_ids, 1);
        }
    }else if (touch.view == self.type3_IV3 ){
        if (self.tapAction) {
            self.tapAction(self.model.pic_ids, 2);
        }
    }
}

@end
